import Vue from 'vue'
import Router from 'vue-router'
import { getUser } from '@/utils/auth'


const Kline = r => require.ensure([], () => r(require('@/views/kline')), 'Kline');
const MHpicture = r => require.ensure([], () => r(require('@/views/mhpicture')), 'MHpicture');
const Advertisement = r => require.ensure([], () => r(require('@/views/advertisement')), 'Advertisement');


const Servercenter = r => require.ensure([], () => r(require('@/views/servercenter')), 'Servercenter');
const Playskill = r => require.ensure([], () => r(require('@/views/playskill')), 'Playskill');

const News = r => require.ensure([], () => r(require('@/views/news')), 'News');
const NewsInfo = r => require.ensure([], () => r(require('@/views/news/newsInfo')), 'NewsInfo');

const Search = r => require.ensure([], () => r(require('@/views/servercenter/search')), 'Search');
const CompanyInfo = r => require.ensure([], () => r(require('@/views/servercenter/companyInfo')), 'CompanyInfo');

const Home = r => require.ensure([], () => r(require('@/views/home')), 'Home');
const Home1 = r => require.ensure([], () => r(require('@/views/home1')), 'Home1');
const Manage = r => require.ensure([], () => r(require('@/views/msg')), 'Manage');

const Article = r => require.ensure([], () => r(require('@/views/msg/article')), 'Article');
const ArticleInfo = r => require.ensure([], () => r(require('@/views/msg/article/articleInfo')), 'ArticleInfo');

const Service = r => require.ensure([], () => r(require('@/views/msg/service')), 'Service');
const ServiceInfo = r => require.ensure([], () => r(require('@/views/msg/service/serviceInfo')), 'ServiceInfo');

const User = r => require.ensure([], () => r(require('@/views/msg/user/index')), 'User');
const UserInfo = r => require.ensure([], () => r(require('@/views/msg/user/userInfo')), 'UserInfo');

const Login = r => require.ensure([], () => r(require('@/views/login')), 'Login');

const FrontMsg = r => require.ensure([], () => r(require('@/views/frontmsg/index')), 'FrontMsg');
const ChangeUser = r => require.ensure([], () => r(require('@/views/frontmsg/changeUser')), 'ChangeUser');
const Recharge = r => require.ensure([], () => r(require('@/views/frontmsg/recharge')), 'Recharge');
const RechargeRecored = r => require.ensure([], () => r(require('@/views/frontmsg/rechargerecored')), 'RechargeRecored');
const ScoreStore = r => require.ensure([], () => r(require('@/views/frontmsg/scorestore')), 'ScoreStore');
const ScoreRecored = r => require.ensure([], () => r(require('@/views/frontmsg/scorerecored')), 'ScoreRecored');


const RechargeCard = r => require.ensure([], () => r(require('@/views/msg/rechargecard/index')), 'RechargeCard');
const RechargeCardInfo = r => require.ensure([], () => r(require('@/views/msg/rechargecard/rechargecardinfo')), 'RechargeCardInfo');

const AdvertisementList = r => require.ensure([], () => r(require('@/views/msg/advertisement')), 'AdvertisementList');
const AdvertisementInfo = r => require.ensure([], () => r(require('@/views/msg/advertisement/advertisementInfo')), 'AdvertisementInfo');

const router = new Router({
  mode: 'history',
  routes: [{
    path: '/',
    name: 'home',
    component: Home
  },{
    path: '/home1',
    name: 'home1',
    component: Home1
  },
  {
    path: '/login/:args',
    name: 'login',
    component: Login
  },
  {
    path: '/register/:args',
    name: 'register',
    component: Login
  }, {
    path: '/kline',
    name: 'kLine',
    component: Kline,
    meta: {
      requiresAuth: true
    }
  }, {
    path: '/mhpicture',
    name: 'mhpicture',
    component: MHpicture,
    meta: {
      requiresAuth: true
    }
  }, {
    path: '/advertisement',
    name: 'advertisement',
    component: Advertisement,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/servercenter',
    name: 'servercenter',
    component: Servercenter,
    meta: {
      requiresAuth: true
    }
  }, {
    path: '/playskill',
    name: 'playskill',
    component: Playskill,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/news',
    name: 'news',
    component: News,
    meta: {
      requiresAuth: true
    }
  }, {
    path: '/newsInfo/:id',
    name: 'newsInfo',
    component: NewsInfo,
  }, {
    path: '/search',
    name: 'search',
    component: Search,
  }, {
    path: '/companyInfo/:args',
    name: 'companyInfo',
    component: CompanyInfo,
  }, {
    path: '/manage',
    name: 'manage',
    component: Manage,
    meta: {
      requiresAuth: 5
    },
    children: [
      {
        path: 'article',
        name: 'article',
        meta: {
          requiresAuth: 5
        },
        component: Article

      },
      {
        path: 'articleinfo',
        name: 'articleinfo',
        meta: {
          requiresAuth: 5
        },
        component: ArticleInfo
      },
      {
        path: 'service',
        name: 'service',
        meta: {
          requiresAuth: 5
        },
        component: Service
      },
      {
        path: 'serviceinfo',
        name: 'serviceinfo',
        meta: {
          requiresAuth: 5
        },
        component: ServiceInfo
      },
      {
        path: 'userList',
        name: 'userList',
        meta: {
          requiresAuth: 5
        },
        component: User
      },
      {
        path: 'userinfo',
        name: 'userinfo', meta: {
          requiresAuth: 5
        },
        component: UserInfo
      },
      {
        path: 'advertisementList',
        name: 'advertisementList', meta: {
          requiresAuth: 5
        },
        component: AdvertisementList
      },
      {
        path: 'advertisementInfo',
        name: 'advertisementInfo', meta: {
          requiresAuth: 5
        },
        component: AdvertisementInfo
      }, 
      {
        path: 'rechargecard',
        name: 'rechargecard',
        meta: {
          requiresAuth: 5
        },
        component: RechargeCard
      },
      {
        path: 'rechargecardinfo',
        name: 'rechargecardinfo',
        meta: {
          requiresAuth: 5
        },
        component: RechargeCardInfo
      },
      { path: '',
        meta: {
        requiresAuth: 5
      }, component: User },
    ]
  },
  {
    path: '/frontmsg',
    name: 'frontmsg',
    component: FrontMsg,
    children: [
      {
        path: 'changuser',
        name: 'changuser',
        meta: {
          requiresAuth: 1
        },
        component: ChangeUser
      },
      {
        path: 'recharge',
        name: 'recharge',
        meta: {
          requiresAuth: 1
        },
        component: Recharge
      },
      {
        path: 'rechargerecored',
        name: 'rechargerecored',
        meta: {
          requiresAuth:1
        },
        component: RechargeRecored
      },
      {
        path: 'scorerecored',
        name: 'scorerecored',
        meta: {
          requiresAuth:1
        },
        component: ScoreRecored
      },
      {
        path: 'scorestore',
        name: 'scorestore',
        meta: {
          requiresAuth:1
        },
        component: ScoreStore
      },

      { path: '', component: ChangeUser },
    ]
  }


  ]
});



Vue.use(Router)
//注册全局钩子用来拦截导航
router.beforeEach((to, from, next) => {
  //获取store里面的token
  //判断要去的路由有没有requiresAuth
  // console.log("路由拦截","to:",to,"from",from,);
  let user = getUser() ? JSON.parse(getUser()) : "";
  // console.log(to,from,Boolean(to.meta.requiresAuth));
  if (to.meta.requiresAuth) {
    if (user) {
      if (to.meta.requiresAuth == 5) {
        if (user.type == 5) next();
        else next({path:from.path});
      }else{
        next()
      }
    } else {
      next({
        path: '/login/first',
        query: {
          redirect: to.fullPath
        } // 将刚刚要去的路由path作为参数，方便登录成功后直接跳转到该路由
      });
    }
  } else {
    next();
  }
});

export default router;
